package com.greateamd.timetableasy.client.mvc.controller;

import com.greateamd.timetableasy.client.Framework;
import com.greateamd.timetableasy.client.domain.AuthType;
import com.greateamd.timetableasy.client.domain.EventType;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.mvc.model.PlanningModel;
import com.greateamd.timetableasy.client.mvc.view.IMainView;
import com.greateamd.timetableasy.client.mvc.view.MainView;
import com.greateamd.timetableasy.client.mvc.view.TimeTableasyPage;
import com.greateamd.timetableasy.domain.Auth;
import com.greateamd.timetableasy.domain.User;

public class MainController implements IMainController<Model,IMainView>{
	
	private static final IMainController instance = new MainController();
	public static IMainController getInstance(){
		return instance;
	}

	private final Model model;
	private final IMainView<Model,IMainController> view;
	
	private MainController() {
		model = new Model();
		view = new MainView<Model, IMainController>(model,this);
	}
	

	private void setHomePage(){
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.HOME)){
			Framework.get().setLocation(TimeTableasyPage.HOME.location);
			view.setHomePage(model);
		}
	}
	
	private void setAddUserPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_USER)){
			Framework.get().setLocation(TimeTableasyPage.ADD_USER.location);
			view.setAddUserPage(model);
		}
	}

	private void setAddCampusPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_CAMPUS)){
			Framework.get().setLocation(TimeTableasyPage.ADD_CAMPUS.location);
			view.setAddCampusPage(model);
		}
	}
	
	private void setAddCoursePage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_COURSE)){
			Framework.get().setLocation(TimeTableasyPage.ADD_COURSE.location);
			view.setAddCoursePage(model);
		}
	}
	
	private void setAddPromotionPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_PROMOTION)){
			Framework.get().setLocation(TimeTableasyPage.ADD_PROMOTION.location);
			view.setAddPromotionPage(model);
		}
	}
	
	private void setAddCursusPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_CURSUS)){
			Framework.get().setLocation(TimeTableasyPage.ADD_CURSUS.location);
			view.setAddCursusPage(model);
		}
	}
	
	private void setAddUniversityPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_UNIVERSITY)){
			Framework.get().setLocation(TimeTableasyPage.ADD_UNIVERSITY.location);
			view.setAddUniversityPage(model);
		}
	}
	
	private void setAddEventUserPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_EVENT_USER)){
			Framework.get().setLocation(TimeTableasyPage.ADD_EVENT_USER.location);
			view.setAddEventPage(model,EventType.USER);
		}
	}
	
	private void setAddEventCampusPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_EVENT_CAMPUS)){
			Framework.get().setLocation(TimeTableasyPage.ADD_EVENT_CAMPUS.location);
			view.setAddEventPage(model,EventType.CAMPUS);
		}
	}
	
	private void setAddEventUniversityPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_EVENT_UNIVERSITY)){
			Framework.get().setLocation(TimeTableasyPage.ADD_EVENT_UNIVERSITY.location);
			view.setAddEventPage(model,EventType.UNIVERSITY);
		}
	}
	
	private void setAddEventPromotionPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_EVENT_PROMOTION)){
			Framework.get().setLocation(TimeTableasyPage.ADD_EVENT_PROMOTION.location);
			view.setAddEventPage(model,EventType.PROMOTION);
		}
	}
	
	private void setAddEventSpPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.ADD_EVENT_SP)){
			Framework.get().setLocation(TimeTableasyPage.ADD_EVENT_SP.location);
			view.setAddEventPage(model,EventType.STUDY_PERIOD);
		}
	}

	private void setEditUserPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.EDIT_USER)){
			Framework.get().setLocation(TimeTableasyPage.EDIT_USER.location);
			view.setEditUserPage(model);
		}
	}
	
	private void setEditUniversityPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.EDIT_UNIVERSITY)){
			Framework.get().setLocation(TimeTableasyPage.EDIT_UNIVERSITY.location);
			view.setEditUniversityPage(model);
		}
	}
	
	private void setUserPlanningPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.PLANNING_USER)){
			Framework.get().setLocation(TimeTableasyPage.PLANNING_USER.location);
			PlanningModel planningModel = new PlanningModel(model);
			//User have teacher auth
			if(model.getUser().getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_TEACHER)))
				view.setTeacherPlanningPage(planningModel);
			else
				view.setUserPlanningPage(planningModel);
		}
	}
	
	private void setPromotionPlanningPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.PLANNING_PROMOTION)){
			Framework.get().setLocation(TimeTableasyPage.PLANNING_PROMOTION.location);
			PlanningModel planningModel = new PlanningModel(model);
			view.setPromotionPlanningPage(planningModel);
		}
	}
	
	private void setStudyPeriodPlanningPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.PLANNING_STUDYPERIOD)){
			Framework.get().setLocation(TimeTableasyPage.PLANNING_STUDYPERIOD.location);
			PlanningModel planningModel = new PlanningModel(model);
			view.setStudyPeriodPlanningPage(planningModel);
		}
	}
	
	private void setCampusPlanningPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.PLANNING_CAMPUS)){
			Framework.get().setLocation(TimeTableasyPage.PLANNING_CAMPUS.location);
			PlanningModel planningModel = new PlanningModel(model);
			view.setCampusPlanningPage(planningModel);
		}
	}
	
	private void setUniversityPlanningPage() {
		if(AuthType.authorizeUserPage(model.getUser(), TimeTableasyPage.PLANNING_UNIVERSITY)){
			Framework.get().setLocation(TimeTableasyPage.PLANNING_UNIVERSITY.location);
			PlanningModel planningModel = new PlanningModel(model);
			view.setUniversityPlanningPage(planningModel);
		}
	}
	
	@Override
	public void onUserChange(final User u) {
		model.setUser(u);
		view.onUserChange();
	}
	
	@Override
	public void onUserChange() {
	}


	public void onPageRequest(final TimeTableasyPage page) {
		switch(page){
			case HOME:
				setHomePage();break;
			case PLANNING_USER:
				setUserPlanningPage();break;
			case PLANNING_PROMOTION:
				setPromotionPlanningPage();break;
			case PLANNING_STUDYPERIOD:
				setStudyPeriodPlanningPage();break;
			case PLANNING_CAMPUS:
				setCampusPlanningPage();break;
			case PLANNING_UNIVERSITY:
				setUniversityPlanningPage();break;
			case ADD_USER:
				setAddUserPage();break;
			case ADD_CAMPUS:
				setAddCampusPage();break;
			case ADD_COURSE:
				setAddCoursePage();break;
			case ADD_CURSUS:
				setAddCursusPage();break;
			case ADD_PROMOTION:
				setAddPromotionPage();break;
			case ADD_UNIVERSITY:
				setAddUniversityPage();break;
			case ADD_EVENT_USER:
				setAddEventUserPage();break;
			case ADD_EVENT_CAMPUS:
				setAddEventCampusPage();break;
			case ADD_EVENT_PROMOTION:
				setAddEventPromotionPage();break;
			case ADD_EVENT_SP:
				setAddEventSpPage();break;
			case ADD_EVENT_UNIVERSITY:
				setAddEventUniversityPage();break;
			case EDIT_USER:
				setEditUserPage();break;
			case EDIT_UNIVERSITY:
				setEditUniversityPage();break;
			default:
				setHomePage();break;
		}
	}

}
